<!DOCTYPE html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp"%>

<html>
<head>
<title>生产入库编辑</title>
<!-- import css and js -->
<%@ include file="/common/metaInput.jsp"%>
<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script src="${ctx}/js/jsp/date.js" type="text/javascript"></script>
<script type="text/javascript">
	var basic_Url = "${ctx}/logic/prostoragein";
	var addressGrid;
	var print=false;
	var obj;
	$(function() {
		obj ={'zd004WhName':$('#zd004WhName').val(),'zd010OtherDate':$('#zd010OtherDate').val(),'zd003PiCode':$('#zd003PiCode').val(),'zd002OriCode':$('#zd002OriCode').val(),'zd015SalesMan':$('#zd015SalesMan').val(),'zd012CustomerName':$('#zd012CustomerName').val(),'zd001InCode':$('#zd001InCode').val(),'zd006Aegnt':'${userName}'};
		$("#inputForm").validate({
			onsubmit : true,
			/*执行ajaxsubmit  */
			submitHandler : function(editform) {

				try {
					url = basic_Url + "/save.do";
					var api = frameElement.api;
					var W = api.opener;
					//getReturnMessage(url, "inputForm", success_function);
					getReturnMessageThree(url, "inputForm", W.searchAP,"",obj,getNeedData(addressGrid.getSelectedRows()),print,"prostoragein");
				} catch (e) {
					alert(e);
				}
			}
		});
		
		if ('${viewflag}' == "add"){
			$("#tr1").hide();
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#print1").hide();
			$("#print2").hide();
		}else if ('${viewflag}' == "tijiao"){
			$("#save").hide();
			$("#save2").hide();
			$("#save3").hide();
            if($("#zd006Aegnt").val()!="${user.name}"){
    			$("#save1").hide();
    			$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
    			$("select").attr("disabled",true);//禁用SELECT标签
    			$("textarea").attr("disabled",true);//禁用TEXTAREA标签
               }  
		}else if ('${viewflag}' == "shenpi"){
			$("#save").hide();
			$("#save1").hide();
			$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
			$("select").attr("disabled","disabled");//禁用SELECT标签
			$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
		}else if ('${viewflag}' == "tongguo"){
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#save").hide();
			$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
			$("select").attr("disabled","disabled");//禁用SELECT标签
			$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
		}
		
		var column1 = [       
				    { display: 'id', name: 'id',type: 'string',hide:true},
				    { display: '物料id', name: 'zd002MaterialsId',width:1,hide:true},//使用selecturl时必须设置该属性
				    { display: '<font color="gray">产品编码</font>', name: 'zd002MaterialsCode', width: 120},
		            { display: '<font color="gray">产品名称</font>', name: 'zd002MaterialsName',width: 120},
		            { display: '<font color="gray">产品型号</font>', name: 'zd004MaterialsModel', isSort : true, width: 100},
					{ display: '<font color="gray">产品尺寸</font>', name: 'zd015MaterialsSize', width: 160},
					{ display: '<font color="gray">产品颜色</font>', name: 'zd016MaterialsColor', width: 100},
					{ display: '<font color="gray">产品材质</font>', name: 'zd017MaterialsQuality', width: 100},
		            { display: '<font color="gray">基本单位</font>', name: 'zd006Unit' ,width: 100},
		            { display: '<font color="gray">订单数量</font>', name: 'zd018PlanQuantity',width: 150},
		            { display: '<font color="gray">已入库数量</font>', name: 'zd019WareQuantity',width: 150},
		            { display: '入库数量<font color="red">*</font>', name: 'zd007Quantity',editor : {type :'float'},width: 150},
		            { display: '单价',hide:true, name: 'zd009Price'},
		            { display: '反审数量<font color="red">*</font>', name: 'zd007BackQuantity',width: 150,render:function(item){
		            	if(isNaN(item.zd007BackQuantity)){
		            		return '0.00';
		            	}else{
		            		return item.zd007BackQuantity;
		            	}
		            }},
		            { display: '描述', name: 'zd014MaterialsDescription',width: 150,editor : {type :'text'}}
		           ];
		            var column2 = [ 
				    { display: 'id', name: 'id',type: 'string',hide:true},
				    { display: '物料id', name: 'zd002MaterialsId',width:1,hide:true},//使用selecturl时必须设置该属性
				    { display: '<font color="gray">产品编码</font>', name: 'zd002MaterialsCode', width: 120},
		            { display: '<font color="gray">产品名称</font>', name: 'zd002MaterialsName',width: 120},
		            { display: '<font color="gray">产品型号</font>', name: 'zd004MaterialsModel', isSort : true, width: 100},
					{ display: '<font color="gray">产品尺寸</font>', name: 'zd015MaterialsSize', width: 160},
					{ display: '<font color="gray">产品颜色</font>', name: 'zd016MaterialsColor', width: 100},
					{ display: '<font color="gray">产品材质</font>', name: 'zd017MaterialsQuality', width: 100},
		            { display: '<font color="gray">基本单位</font>', name: 'zd006Unit' ,width: 100},
		            { display: '<font color="gray">订单数量</font>', name: 'zd018PlanQuantity',width: 150},
		            { display: '<font color="gray">已入库数量</font>', name: 'zd019WareQuantity',width: 150},
					{ display: '单价',hide:true, name: 'zd009Price'},
					{ display: '入库数量<font color="red">*</font>', name: 'zd007Quantity',width: 150},
					{ display: '反审数量<font color="red">*</font>', name: 'zd007BackQuantity',width: 150,render:function(item){
		            	if(isNaN(item.zd007BackQuantity)){
		            		return '0.00';
		            	}else{
		            		return item.zd007BackQuantity;
		            	}
		            }},
		            { display: '描述', name: 'zd014MaterialsDescription',width: 150}
		   			];
		               var column3 = null;
		               if ('${xx}' == 1){
		               	column3 = column1;
		               }else 
		               	column3 = column2;
		               addressGrid = $("#dataGrid1").ligerGrid({
		           		 columns: column3,
		           	                    enabledEdit : true,enabledSort:true,
		           	                    usePager: true,
		           	      				width : '99%',
		           	      				pageSize:1000,
		           	      				pageSizeOptions:[1000],
		           	      				parms:[{ name : 'id' , value : '${oi.id}'}],
		    	      		        	url : basic_Url+"/griditems.do",
		           	      				rownumbers : false,
		           	      				checkbox : true,
		           	                    height : '90%',
		           		toolbar : {
		           			items : [ {
		           				text : '删除',
		           				click : function() {
		           					if ('${xx}' == 1){
		           					addressGrid.deleteSelectedRow();
		           					}
		           				},
		           				img : '${ctx}/js/ligerUI/skins/icons/delete.gif'
		           			}]
		           		}});
	});
	function submitForm(param) {
	 	$("#zd011BillStatus").val(param);
	 	//$("#zd004WhName").val($("#zd004WhCode").find("option:selected").text());
		var data = $("#dataGrid1").ligerGetGridManager().getSelectedRows();
		for(var i = 0 ; i<data.length;i++){
			data[i].zd020Order=i+"";
		}
	 	$("#addressInfos").val(JSON.stringify(data));
		if(data.length==0){
			$.dialog.alert("您入库的产品不能为空,请选择你要您要入库的产品");
	   	 	return false;
		}
		var ba=true;
		if(data !=null){
			for (var i = 0;i < data.length;i++){
				var row = data[i];
				if(undefined == row.zd007Quantity
						|| isNaN(row.zd007Quantity)
						|| row.zd007Quantity <= 0){
		           	 $.dialog.alert("您第"+(i+1)+"行的“入库数量”不合法");
		           	 ba=false;
		           	 return false;
		        }
				if(row.zd018PlanQuantity-row.zd019WareQuantity+row.zd007Quantity<0){
		           	 $.dialog.alert("您第"+(i+1)+"行的“入库数量”大于“订单数量”不合法");
		           	 ba=false;
		           	 return false;
		        }
			}
			
		}
		
		//设置ajax全局同步
		jQuery.ajaxSettings.async = false;
		if(ba){
			$("#submitFlag").val(param);
			if(param=="PROSTORIN_SUBMIT"){
				print=true;
			}
			$("#inputForm").submit();
		}
	}

	/* function getA(){
		$("#zd004WhName").val($("#zd004WhCode").find("option:selected").text());
	} */
	
	function check(){
		$.ajax({
		      url: "${ctx}/logic/prostoragein/updateState.do",
		      global: false,
		      type: "POST",
		      dataType: "json",
		      data: ({
		    	   rows:$("#id").val()
		      }),		     
		      success: function(msg){
		    	  if(msg.msg=="SUCCESS"){
		    		  $.dialog.alert("驳回成功",function(){close();});
		    	  }
		    	  if(msg.msg=="ERROR"){
		    		  $.dialog.alert("操作失败",function(){close();});
		    	  }
		   }
		});
	}
    
	function close(){
		var api = frameElement.api;
		var W = api.opener;
		CloseDialog();
		W.searchAP();
	}
	
	//生产计划单列表
	function PlanWindow(){
			getDG().dialog(
					{
						title : '选择生产计划单',
						width : 650,
						height : 350,
						content : 'url:' + sys_context+ '/public/windowalert/plan.do?type=1',    
						lock : true,
						max : true,
						min : true,
						ok : function() {
							var rows = this.content.getSelected();
							if(rows==null){
								$.dialog.alert("请选择数据!");
								return;
							}else{
								if(rows.length>1){
									$.dialog.alert("请选择数据!");
									return;
								}else{
									$(rows).each(function() {
										$("#zd012CustomerId").val(this.zd004CustomerId);
										$("#zd012CustomerCode").val(this.zd004CustomerCode);
										$("#zd012CustomerName").val(this.zd004CustomerName);
										$("#zd002OriCode").val(this.zd001FormCode);
										$("#zd003PiCode").val(this.zd003PiCode);
										$("#zd013CurrencyCode").val(this.zd010CurrencyCode);
										$("#zd013CurrencyName").val(this.zd010CurrencyName);
										$("#zd014ExchangeRate").val(this.zd011ExchangeRate);
										$("#zd015SalesMan").val(this.zd007SalesMan);
										var url=basic_Url+"/gridBomItems.do?id="+this.id;
										multipleSearch("#dataGrid1",true,"",url);
									});
								}
							}
							this.content.Close();
							return false;
						},
						cancelVal : '关闭',
						cancel : true
					}).zindex();
	}
	
	//入库仓
	function  SotrageinfoWindow() {
		getDG().dialog(
				{
					title : '选择仓库',
					width : 650,
					height : 350,
					content : 'url:' + sys_context + '/public/windowalert/sotrageinfo.do',    
					lock : true,
					max : true,
					min : true,
					ok : function() {
						var rows = this.content.getSelected();
						if(rows==null){
							$.dialog.alert("请选择数据");
							return;
						}
						else{
							if(rows.length>1){
								$.dialog.alert('只能选择一行数据!');
								return;
							}else{
								$(rows).each(function() {
									$("#zd004Whid").val(this.id);
									$("#zd004WhCode").val(this.zd002Code);
									$("#zd004WhName").val(this.zd003Name);
							  });
							}
						}
						this.content.Close();
						return false;
					},
					cancelVal : '关闭',
					cancel : true
				}).zindex();
	}
	

	function PrintView(type){
		var isprint=false;
		if(type==2){
			isprint=true;
		}
		window.parent.parent.loadPrint(obj,getNeedData(addressGrid.getData()),"prostoragein",isprint);
		isprint=false;
	}
	function getNeedData(data){
		var arr= new Array();
		for(var i=0;i<data.length;i++){
			if(!isNaN(data[i].zd007BackQuantity)){
				data[i].zd007Quantity = parseFloat(data[i].zd007Quantity)-parseFloat(data[i].zd007BackQuantity)
			}
			if(parseFloat(data[i].zd007Quantity)!=0){
				arr.push(data[i]);
			}
		}
		return arr;
	}
</script>

</head>

<body>
<input type="hidden" name="xilie" id='xilie' value="${xilie}"/>
	<form id="inputForm" action="" method="post">
		<input type="hidden" id="id" name="id" value="${oi.id}" />
		<input type="hidden" id="addressInfos" name="addressInfos"/>
		<input type="hidden" id="bizVersion" name="bizVersion" value="${oi.bizVersion}"/>
		<input type="hidden" id="zd011BillStatus" name="zd011BillStatus" value="${oi.zd011BillStatus}"/>
		<input type="hidden" id="zd013CurrencyName" name="zd013CurrencyName" value="${oi.zd013CurrencyName}"/>
		<input type="hidden" id="zd013CurrencyCode" name="zd013CurrencyCode" value="${oi.zd013CurrencyCode}"/>
		<input type="hidden" id="zd014ExchangeRate" name="zd014ExchangeRate" value="${oi.zd014ExchangeRate}"/>
		<!-- 用于标识提交按钮 -->
		<input type="hidden" id="submitFlag" value="" />
		<div class="pageContent">
			<div class="panel">
				<table class="TableBlock">
					<tr id="tr1">
					 	<td>生产入库单:</td>
						<td  colspan="3"><input class="inp_width_98p" type="text" id="zd001InCode" name="zd001InCode" readonly="readonly"  value="${oi.zd001InCode}" /></td>
					</tr>
					<tr>
						<td nowrap>生产计划订单:</td>
						<td><input id="zd002OriCode" name="zd002OriCode" readonly="readonly" style="width:90%;" class="required" value="${oi.zd002OriCode}"></input>
						<input type="button" value="..." onclick="PlanWindow();" />
						</td>
					 	<td nowrap>PI单号:</td>
						<td><input id="zd003PiCode" name="zd003PiCode" readonly="readonly" class="inp_width_98p required" value="${oi.zd003PiCode}"></input>
						</td>
					</tr>
					<tr>
					 	<td nowrap>客户编码:</td>
						<td><input id="zd012CustomerCode" name="zd012CustomerCode" readonly="readonly" class="inp_width_98p required" value="${oi.zd012CustomerCode}"></input>
						</td>
					 	<td nowrap>客户名称:</td>
					 		
						<td><input id="zd012CustomerName" name="zd012CustomerName" readonly="readonly" class="inp_width_98p required" value="${oi.zd012CustomerName}"></input>
							<input id="zd012CustomerId" type="hidden" name="zd012CustomerId" value="${oi.zd012CustomerId}"></input>
						</td>
					</tr>
					<tr>
					 	<td nowrap>入库仓库:</td>
						<td>
							<input type="hidden" id="zd004WhCode" name="zd004WhCode" value="${oi.zd004WhCode}"/>
   		                     <input type="text" id="zd004WhName" name="zd004WhName" value="${oi.zd004WhName}" class="inp_width_98p required"/>
	   		            	 <input type="hidden" style="width:200px;" id="zd004Whid" name="zd004Whid" value="${oi.zd004Whid}" />
   		        			<input type="button" value="..." onclick="SotrageinfoWindow();" />
						</td>
					 	<td nowrap>入库时间:</td>
						<td><input id="zd010OtherDate" name="zd010OtherDate" class="inp_width_98p required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" value="<fmt:formatDate value="${oi.zd010OtherDate}" pattern="yyyy-MM-dd" type="both" />" /></td>
						 	 <c:if test="${oi.zd010OtherDate == null}">
									<script>
										var date = new Date();
										$("#zd010OtherDate").val(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
									</script>
							 </c:if>
					</tr>
					<tr>
						<td nowrap>业务员:</td>
						<td> 
							<input type="text" id="zd015SalesMan" name="zd015SalesMan" class="inp_width_98p required" value="${oi.zd015SalesMan}" />
						</td>
						<td nowrap>制单人:</td>
						<td> 
							<input type="text" id="zd006Aegnt" name="zd006Aegnt" class="inp_width_98p" value="${user.name}" readonly="readonly"/>
							<c:if test="${oi.zd006Aegnt != null}">
								<script>
									$("#zd006Aegnt").val("${oi.zd006Aegnt}");
								</script>
							</c:if>
						</td>
					</tr>
					<tr>
						<td nowrap>备注:</td>
						<td colspan="3"><input class="inp_width_98p"  type="text" id="zd007Remark" name="zd007Remark"  value="${oi.zd007Remark}" /></td>
					</tr>
					<tr>
						<td colspan="4" style="margin-bottom: 10px;"><div
								id="dataGrid1"></div></td>
					</tr>
				</table>
			</div>
			<div class="formBar">
				<ul>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm('PROSTORIN_SAVE')">保存</button></div></div></li>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save1" onclick="submitForm('PROSTORIN_SUBMIT')">提交</button></div></div></li>
		        <webres:displaycontroller elementId="LOGIC_PROIN_SC_SP" description="生产入库单审批">
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save2" onclick="submitForm('PROSTORIN_END')">审核通过</button></div></div></li>
			    </webres:displaycontroller>
			    <webres:displaycontroller elementId="LOGIC_PROIN_SC_BH" description="生产入库单驳回">   
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save3" onclick="check()">驳回</button></div></div></li>
		       </webres:displaycontroller>
				<li id="print1"><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(1);">打印预览</button></div></div></li>
		      	<li id="print2"><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(2);">打印</button></div></div></li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
				</ul>
			</div>
		</div>
	</form>
</body>
</html>
